package xyz.quanguanjia.masterboy;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import xyz.quanguanjia.masterboy.adapter.HomeListAdapter;
import xyz.quanguanjia.masterboy.decoration.HomeItemDecoration;

public class MBDPostListActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<MBDGroupDetailModel> details;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_list);

        Toolbar toolbar = findViewById(R.id.toolbar);
        MBDToolbarUtils.setCenterTitle(toolbar, "我的发布");
        MBDToolbarUtils.backButtonClicked(toolbar, new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });

        SwipeRefreshLayout swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        this.recyclerView = findViewById(R.id.recyclerView);

        // 设置下拉刷新监听器
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 在这里执行下拉刷新的操作
                // 当刷新完成后，调用 swipeRefreshLayout.setRefreshing(false); 结束刷新状态
                MBDPostListActivity.this.refreshData();
                swipeRefreshLayout.setRefreshing(false);
            }
        });

        // 设置上拉加载更多监听器
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // 在这里判断是否到达列表底部，触发加载更多的操作
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(MBDApplication.getContext());
        recyclerView.setLayoutManager(layoutManager);
        HomeItemDecoration hi = new HomeItemDecoration(new HomeItemDecoration.HomeItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(MBDPostListActivity.this, MBDPostDetailActivity.class);
                intent.putExtra("detail", MBDPostListActivity.this.details.get(position));
                startActivity(intent);
            }
        });
        recyclerView.addItemDecoration(hi);
        recyclerView.addItemDecoration(new DividerItemDecoration(MBDApplication.getContext(), LinearLayoutManager.VERTICAL));

        this.refreshData();
    }

    private void doRequest(MBDPostListActivity.CallBack callBack) {

        HashMap<String,String > params = new HashMap<String,String>();
        params.put("pageNum","1");
        MBDHttpClient.shared().doRequest(ServiceType.SERVICE_POST_LIST, params, new MBDHttpClient.Callback() {
            @Override
            public void completion(boolean success, Error error, MBDModelResponse response) {
                if (success){
                    ArrayList list = (ArrayList) response.data;

                    ArrayList resultArrayList = new ArrayList<MBDGroupDetailModel>();
                    for (int i = 0; i < list.size(); i++) {
                        LinkedTreeMap<String,Object> linkedTreeMap = (LinkedTreeMap<String, Object>) list.get(i);
                        Gson gson = new Gson();
                        String jsonString = gson.toJson(linkedTreeMap);
                        MBDGroupDetailModel detail= gson.fromJson(jsonString, MBDGroupDetailModel.class);
                        resultArrayList.add(detail);
                    }

                    callBack.completion(resultArrayList);
                }

            }
        });

    }

    public void refreshData(){
        this.doRequest(new MBDPostListActivity.CallBack() {
            @Override
            public void completion(List<MBDGroupDetailModel> details) {

                MBDPostListActivity.this.details = details;
                // 处理成功的响应
                HomeListAdapter adapter = new HomeListAdapter(details);
                MBDPostListActivity.this.recyclerView.setAdapter(adapter);
            }
        });
    }


    public interface CallBack {
        public void completion(List<MBDGroupDetailModel> details);
    }

}
